package com.zhaungjie.base.utils;

import java.util.Collection;
import java.util.Objects;

/**
 * 集合实用程序
 *
 * @author zhuangjie
 * @date 2024/05/01
 */
public class CollectionUtils {
    /**
     * 集合是否为空
     *
     * @param collection        集合
     * @param ignoreNullElement 是否忽略null元素
     * @return boolean
     */
    public static boolean isEmpty(Collection<?> collection, boolean ignoreNullElement) {
        return collection == null || collection.isEmpty() || (ignoreNullElement && collection.stream().anyMatch(Objects::isNull));
    }

    /**
     * 集合是否为空
     * @param collection 集合
     * @return boolean
     */
    public static boolean isEmpty(Collection<?> collection) {
        return isEmpty(collection,false);
    }

    /**
     * 忽略空元素是否为空
     * @param collection 集合
     * @return boolean
     */
    public static boolean isEmptyIgnoreNullElement(Collection<?> collection) {
        return isEmpty(collection,true);
    }
    /**
     * 集合是否不为空
     * @param collection 集合
     * @return boolean
     */
    public static boolean isNotEmpty(Collection<?> collection) {
        return !isEmpty(collection);
    }

    public static <T> T[] toArray(Collection<T> collection) {
        if (collection == null) return null;
        @SuppressWarnings("unchecked")
        T[] targetArray = (T[]) collection.toArray();
        return targetArray;
    }


}
